// ClipboardDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Clipboard.h"
#include "ClipboardDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CClipboardDlg dialog




CClipboardDlg::CClipboardDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClipboardDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClipboardDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_listCtrl);
}

BEGIN_MESSAGE_MAP(CClipboardDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_DRAWCLIPBOARD()
	ON_BN_CLICKED(IDOK, &CClipboardDlg::OnBnClickedOk)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CClipboardDlg::OnNMDblclkList1)
	ON_BN_CLICKED(IDCANCEL, &CClipboardDlg::OnBnClickedCancel)
	ON_NOTIFY(NM_RCLICK, IDC_LIST1, &CClipboardDlg::OnNMRClickList1)
	ON_COMMAND(ID_SETTOCLIPBOARD, &CClipboardDlg::OnSettoclipboard)
	ON_COMMAND(ID_ERASE_CLIPBOARD, &CClipboardDlg::OnSettoclipboard)
	ON_COMMAND(ID_ERASE_ALL, &CClipboardDlg::OnSettoclipboard)
END_MESSAGE_MAP()


// CClipboardDlg message handlers

BOOL CClipboardDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	SetClipboardViewer();
	CRect rect, rect2;
	this->GetWindowRect(&rect);
	::SystemParametersInfo(SPI_GETWORKAREA,0,&rect2,0);
	::SetWindowPos(m_hWnd,HWND_TOPMOST,rect2.Width()-rect.Width(),rect2.Height()-rect.Height(),rect.Width(),rect.Height(),SWP_SHOWWINDOW);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CClipboardDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CClipboardDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CClipboardDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CClipboardDlg::DisplayClipboardText()
{
	CClipManager x;
	CString str;
	x.GetClipboard(this->m_hWnd,str);
	m_listCtrl.InsertItem(0,str,0);
}

void CClipboardDlg::OnDrawClipboard()
{
	CDialog::OnDrawClipboard();

	//do something here, for example
	DisplayClipboardText();

	// TODO: Add your message handler code here
}

void CClipboardDlg::OnBnClickedOk()
{
	//CClipManager x;
	//x.SetClipboard(this->m_hWnd);
	// TODO: Add your control notification handler code here
	//OnOK();
}

void CClipboardDlg::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: Add your control notification handler code here
	//
	//XmlTextReader  teste;
	CString str = m_listCtrl.GetItemText(pNMItemActivate->iItem,0);
	CClipManager x;
	if(str.GetLength())
		x.SetClipboard(m_hWnd,str);
	*pResult = 0;
}

void CClipboardDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CClipboardDlg::OnNMRClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: Add your control notification handler code here
	CMenu menu;
	CPoint point(pNMItemActivate->ptAction);
	CRect rect;
	m_listCtrl.GetWindowRect(&rect);
	VERIFY(menu.LoadMenuW(IDR_MENU1));
	CMenu *popup = menu.GetSubMenu(0);
	popup->TrackPopupMenu(	TPM_LEFTALIGN | TPM_RIGHTBUTTON,
		point.x+rect.left, 
		point.y+rect.top, 
			this);
	*pResult = 0;
}


void CClipboardDlg::OnSettoclipboard()
{
	MSG *pMsg = (MSG*)GetCurrentMessage();
	ASSERT(pMsg);
	if(pMsg)
	{
		if(pMsg->wParam == ID_SETTOCLIPBOARD)
			AfxMessageBox(_T("setclip"));
		else if(pMsg->wParam == ID_ERASE_CLIPBOARD)
			AfxMessageBox(_T("ideraseclip"));
		else if(pMsg->wParam == ID_ERASE_ALL)
			m_listCtrl.DeleteAllItems();
	}
	
}
